package dk.mirasola.systemtraining.bridgewidgets.client.ui.editor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.RenderUtil;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree.CompareOperator;

public class CompareOperatorEditor extends Composite implements LeafValueEditor<CompareOperator> {
    interface CompareOperatorEditorUiBinder extends UiBinder<Widget, CompareOperatorEditor> {
    }

    private static CompareOperatorEditorUiBinder uiBinder = GWT.create(CompareOperatorEditorUiBinder.class);

    @UiField
    ListBox listBox;

    public CompareOperatorEditor() {
        initWidget(uiBinder.createAndBindUi(this));
        for (CompareOperator compareOperator : CompareOperator.values()) {
            listBox.addItem(compareOperator.getSymbol());
        }
    }

    @Override
    public void setValue(CompareOperator value) {
        for (int i = 0; i < listBox.getItemCount(); i++) {
            if (listBox.getValue(i).equals(value.getSymbol())) {
                listBox.setSelectedIndex(i);
                break;
            }
        }
    }

    @Override
    public CompareOperator getValue() {
        return CompareOperator.valueOfSymbol(listBox.getValue(listBox.getSelectedIndex()));
    }
}
